from flask import Flask, render_template, request,session
from flask_session import Session
from frameworks.utils.JsonResult import *
from frameworks.cores.LoadClass import *
from frameworks.cores.Config import *
from frameworks.utils.JsonResult2 import *
import json
import os

server = Flask(__name__)

from gevent import monkey

# 将python标准的io方法，都替换成gevent中的同名方法，遇到io阻塞gevent自动进行协程切换
monkey.patch_all()

def create_app():
    app = Flask(__name__)
    return app


app = create_app()
app.config['JSON_AS_ASCII']=False
app.config['SESSION_TYPE'] = 'filesystem'

Session(app)

basepath = os.path.join(os.path.dirname(__file__))
config = Config(basepath)

apimap = config.getApi()
env = apimap["base"]
print(env)

print(basepath)

jsonResult = JsonResult2()

loader = LoadClass()

from views.common import common
from views.alert_manage import alert_manage
from views.api_manage import api_manage
from views.app_images import app_images
from views.clusters import clusters
from views.log_manage import log_manage
from views.projects import projects
from views.respostory_manage import respostory_manage
from views.servers import servers
from views.dicts import dicts

app.register_blueprint(common, url_prefix='/common')
app.register_blueprint(alert_manage, url_prefix='/alert_manage')
app.register_blueprint(api_manage, url_prefix='/api_manage')
app.register_blueprint(app_images, url_prefix='/app_images')
app.register_blueprint(clusters, url_prefix='/clusters')
app.register_blueprint(log_manage, url_prefix='/log_manage')
app.register_blueprint(projects, url_prefix='/projects')
app.register_blueprint(respostory_manage, url_prefix='/respostory_manage')
app.register_blueprint(servers, url_prefix='/servers')
app.register_blueprint(dicts, url_prefix='/dicts')

@app.route(env+"/",methods=['GET', 'POST'])
def index():
    if session.get("username") != None:
        return render_template('index.html')
    else:
        return render_template("login.html")

@app.route(env+"/login/api",methods=['POST'])
def loginapi():
    quert_json = request.get_data(as_text=True)
    userinfo = json.loads(quert_json)
    session['username'] = userinfo["username"]
    return JsonResult.setOk("登录成功","{}")

if __name__ == "__main__":
    app.run()
